package icom.icss.controller;

import icom.icss.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "DeleteUserServlet", urlPatterns = {"/delete"})
public class DeleteUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String s1 = request.getParameter("s");
        String count = request.getParameter("count");
        String currentPage = request.getParameter("currentPage");

        UserServiceImpl userService = new UserServiceImpl();
        if (s1 != null) {
            String[] split = s1.split(",");

            try {
                if (split != null) {
                    for (int i = 0; i < split.length; i++) {
                        userService.DeleteByname(split[i]);
                    }
                }
                response.sendRedirect("page?currentPage=" + currentPage + "&count=" + count);
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else {
            try {
                userService.DeleteByname(username);
                response.sendRedirect("page?currentPage=" + currentPage + "&count=" + count);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
